﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using LNUGame.View;
using LNUGame.Model;
using LNUGame.Model.Menu;

namespace LNUGame.Controller
{
    /// <summary>
    /// Controls everything related to the menus
    /// </summary>
    class MenuController : IController
    {
        private MenuModel m_currentMenu;
        private MainMenu m_mainMenu;
        private ControlsMenu m_controlsMenu;

        private MenuView m_view;
        private MenuInputHandler m_inputHandler;

        private bool m_changeController;
        private bool m_startNewGame;

        /// <summary>
        /// Main constructor.
        /// Creates instances to handle the view, menus and input.
        /// </summary>
        /// <param name="a_manager">The GraphicsDeviceManager to use</param>
        /// <param name="a_contentManager">The ContentManager to use</param>
        public MenuController(GraphicsDeviceManager a_manager, ContentManager a_contentManager)
        {
            m_mainMenu = new MainMenu();
            m_controlsMenu = new ControlsMenu();
            m_currentMenu = m_mainMenu;

            m_view = new MenuView(a_manager, a_contentManager);
            m_inputHandler = new MenuInputHandler();
        }

        /// <summary>
        /// Draws the menu
        /// </summary>
        /// <param name="a_elapsedTime">How long time since the game was started</param>
        /// <param name="a_graphicsDevice">The GraphicsDevice to use</param>
        public void Draw(float a_elapsedTime, GraphicsDevice a_graphicsDevice)
        {
            m_view.DrawMenu(a_graphicsDevice, m_currentMenu.MenuOptions, m_currentMenu.SelectedOption);
        }

        /// <summary>
        /// Updates the menu depending on the currently selected or choosen option
        /// </summary>
        /// <param name="a_elapsedTime">How long time since the game was started</param>
        public void Update(float a_elapsedTime)
        {
            MenuInputHandler.UserInput userInput = m_inputHandler.GetUserInput();

            if (userInput == MenuInputHandler.UserInput.NOTHING)
            {
                return;
            }
            else if (userInput == MenuInputHandler.UserInput.CHOOSE_CURRENT)
            {
                switch (m_currentMenu.SelectedOption)
                {
                    case MenuModel.MenuOption.START_GAME:
                        m_startNewGame = true;
                        m_changeController = true;
                        break;
                    case MenuModel.MenuOption.CONTROLS:
                        m_currentMenu = m_controlsMenu;
                        break;
                    case MenuModel.MenuOption.EXIT_GAME:
                        m_currentMenu.ExitGame();
                        break;
                    case MenuModel.MenuOption.BACK:
                        m_currentMenu = m_mainMenu;
                        break;
                }
            }
            else if(userInput == MenuInputHandler.UserInput.MOVE_UP)
            {
                m_currentMenu.MoveSelectionUp();
            }
            else if (userInput == MenuInputHandler.UserInput.MOVE_DOWN)
            {
                m_currentMenu.MoveSelectionDown();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>True if the controller changed, false otherwise</returns>
        public bool DidPlayerChangeController()
        {
            bool b = m_changeController;
            m_changeController = false;
            return b;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>True if a new game should be started, false otherwise</returns>
        public bool StartNewGame()
        {
            bool b = m_startNewGame;
            m_startNewGame = false;
            return b;
        }
    }
}
